package com.songlanyun.modules.order.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.entity.SaleServiceGoodsCard;
import com.songlanyun.modules.order.model.dto.CheckServiceGoodsDTO;
import com.songlanyun.modules.order.model.dto.RenewalServiceGoodsDTO;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.order.service.SaleServiceGoodsCardService;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.user.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.order.entity.SaleServiceGoodsVerificationRecord;
import com.songlanyun.modules.order.service.SaleServiceGoodsVerificationRecordService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;



/**
 * 服务商品订单核销使用记录
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2021-03-02 16:01:16
 */
@RestController
@RequestMapping("admin/sgoodsverification")
public class ServiceGoodsVerificationAdminController extends AbstractController {
    @Autowired
    private SaleServiceGoodsVerificationRecordService saleServiceGoodsVerificationRecordService;

    @Autowired
    private SaleServiceGoodsCardService saleServiceGoodsCardService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;

    /**
     * 核销记录列表
     */
    @RequestMapping("/list/{orderId}/{childOrderId}")
    //@RequiresPermissions("order:saleservicegoodsverificationrecord:list")
    public R list(@PathVariable("orderId") Long orderId,@PathVariable("childOrderId") Long childOrderId){
        List<SaleServiceGoodsVerificationRecord> list = saleServiceGoodsVerificationRecordService.loadByOrderIdAndChildId(orderId, childOrderId);

        return R.ok().put("list", list);
    }




    /**
     * 店铺管理员核销服务
     */
    @SysLog("店铺管理员核销服务")
    @PostMapping("/check")
    public R check(@RequestBody CheckServiceGoodsDTO dto){
        SysUserEntity userInfo = getUserInfo(true);
        ChildOrder childOrder = saleServiceGoodsVerificationRecordService.check(userInfo.getUsername(), getShopId(true), dto);
        if(childOrder.getEnd())
        {
            sendOrderOrRightsOrderMsgService.checkServiceOrderCompleteSendMsg(childOrder);
        }
        return R.ok();
    }


    /**
     * 核销服务核销之前加载订单明细
     */
    @GetMapping("/loadorder/{verificationCode}")
    public R loadOrder(@PathVariable("verificationCode")String verificationCode){
        SaleServiceGoodsCard saleServiceGoodsCard = saleServiceGoodsCardService.loadByCode(verificationCode, true);
        Order order = orderService.getById(saleServiceGoodsCard.getOrderId());

        if (order == null) {
            throw new RRException(OrderException.ORDER_NOT_EXISTS);
        }

        //服务订单不属于您的店铺
        if(!order.getShopId().equals(getShopId(true)))
        {
            throw new RRException(OrderException.SERVICE_CARD_SHOP_NOT_SAME);
        }

        if(!order.getOrderType().equals(OrderConstant.OrderType.SERVICE_ORDER))
        {
            throw new RRException(OrderException.NOT_SERVICE_ORDER);
        }



        List<ChildOrder> childOrderList = order.getChildOrderList(true);
        childOrderList.forEach(o->{
            o.loadServiceCard().loadServiceItems();
        });
        order.loadServiceOrderContacts();

        order.loadOperator(getShopId());
        return R.ok().put("data", order);

    }


    /**
     * 商家对服务订单续期
     */
    @SysLog("商家对服务订单续期")
    @PostMapping("/renewal")
    public R renewal(@RequestBody RenewalServiceGoodsDTO dto){
        ValidatorUtils.validateEntity(dto);
        saleServiceGoodsVerificationRecordService.renewal(getShopId(true),dto);
        return R.ok();
    }




}
